package com.wgl.gsc.aop;

import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import com.wgl.gsc.common.SysInfo;

@Aspect
@Component
public class ServiceLogger {
	private static Logger logger = Logger.getLogger(SysInfo.class);

	// 声明该方法是一个前置通知：在目标方法开始之前执行
	// @Before("execution(public int
	// com.yl.spring.aop.impl.ArithmeticCalculatorImpl.add(int, int))")
	@Before("execution(* com.wgl.gsc.service.impl.*.*(..))")
	public void beforeMethod(JoinPoint joinpoint) {
		String methodName = joinpoint.getSignature().getName();
		List<Object> args = Arrays.asList(joinpoint.getArgs());
		logger.info("The method " + methodName + " begins with " + args);
	}

	// 后置通知：在目标方法执行后（无论是否发生异常），执行的通知
	// 在后置通知中，还不能访问目标方法执行的结果
	@After("execution(* com.wgl.gsc.service.impl.*.*(..))")
	public void afterMethod(JoinPoint joinPoint) {
		String methodName = joinPoint.getSignature().getName();
		List<Object> args = Arrays.asList(joinPoint.getArgs());
		logger.info("The method " + methodName + " end with " + args);
	}
}
